<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Manage\Controller;

//数据分页
use Think\Page;

/**
 * 文章页面控制器
 * 主要获取首页聚合数据
 */
class ArticleController extends ManageController {
	
	public function _initialize(){
	    parent::_initialize();
	    $this->assign('left_menu','ARTICLE');
	}

	/**
	 * 显示公告列表信息
	 */
	public function index(){
		
		$announcementLogic = D('Announcement','Logic');
		
		$count = $announcementLogic->where($map)->count();
		$page = new Page($count,20);
		$show       = $page->show();// 分页显示输出
		// 		dump($show);exit();
		// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		$list = $announcementLogic->where($map)->order('id DESC')->limit($page->firstRow.','.$page->listRows)->select();
		$this->assign('lists',$list);// 赋值数据集
		$this->assign('page',$show);// 赋值分页输出
	    $this->display();
	}
    
    
    /**
     * 添加公告
     */
	public function articleAdd(){
		//如果是添加文章信息
		if(IS_POST){
			
			$announcementLogic = D('Announcement','Logic');
			
		    $id = intval(I('post.id'));
		    $isred = I('post.isred');
		    $usertype = I('post.usertype','1','intval');
		    $isred = intval($isred) == 1 ? 1 : 0;
		    $usertype = ($usertype) == 1 ? 1 : (($usertype) == 2 ? 2 : 3);
		    //公告内容
		    $content = I('post.content');
		    
		    if(empty($content)){
		        $this->error('文章内容必须填写');
		    }
		    
		    $data = array(
		        'isred' => $isred,
		    	'content'=>$content,
		    	'usertype'=>$usertype,
		    );
		    
		    if($id > 0){
		        $boolean = $announcementLogic->where(array('id'=>$id))->save($data);
		    }else{
		    	$data['createtime'] = time();
		    	$boolean = $announcementLogic->add($data);
		    }
// 		    dump($announcementLogic->getLastSql());exit;
		    if($boolean){
		        $this->success('操作成功');
		    }else{
		        $this->error('操作失败');
		    }
		    
		}else{
			$id = intval(I('get.id'));
			if($id > 0){
				$announcementLogic = D('Announcement','Logic');
				$announcement = $announcementLogic->detail($id);
				$this->assign('item',$announcement);
			}
			$this->display();
		}
	   
	}
    
	public function del($id){
		$boolean = false;
	    if($id > 0){
	    	$announcementLogic = D('Announcement','Logic');
	    	$boolean = $announcementLogic->del($id);
	    }
	    
	    if($boolean){
	        $this->success('操作成功');
	    }else{
	        $this->error('操作失败');
	    }
	}

}